<?PHP
namespace DB
{
   class Aurora extends DataBase // DataBase name
   {
   }
}

namespace DB\Sec
{
   use \DB;

   class AccountsRepository implements DB\RepositoryInterface
   {
      private $columns = "ACCOUNT_ID,
         USER_ID,
         WEB_ACCOUNT_ID,
         NAME,
         MIDDLE_NAME,
         FIRST_NAME,
         ID,
         PASSWORD,
         EMAIL_ADDRESS,
         IS_LINKED_TO_EMP,
         IS_LINKED_TO_CONTACT,
         DELETED,
         ENABLED,
         PW_MUST_CHANGE,
         FAILED_LOGINS,
         LDAPID,
         PROFILE_ID,
         LDAP_OBJECT_GUID,
         ACCOUNT_TYPE,
         CREATION_ACCOUNT_ID,
         CREATION_TIMESTAMP,
         LATEST_MOD_ACCOUNT_ID,
         LATEST_MOD_TIMESTAMP,
         EO_USE_OSR, ROWNUM";
            
      public function find($where, $args = array(), $from = 0, $count = 10)
      {
         $minRow = $from;
         $maxRow = $from + $count;
            
         $query = sprintf("SELECT z2.* FROM ( SELECT z1.*, ROWNUM AS zend_db_rownum FROM (

    SELECT {$this->columns} FROM SEC_ACCOUNTS
    WHERE ($where)

   ) z1 ) z2 WHERE z2.zend_db_rownum BETWEEN %d AND %d", $minRow, $maxRow);
         $q = DB\Table::Select($query, $args);
         
         return $q;
      }
      
      public function findAll($from = 0, $count = 10)
      {
         $minRow = $from;
         $maxRow = $from + $count;
            
         $query = sprintf("SELECT z2.* FROM ( SELECT z1.*, ROWNUM AS zend_db_rownum FROM (

    SELECT {$this->columns} FROM SEC_ACCOUNTS

   ) z1 ) z2 WHERE z2.zend_db_rownum BETWEEN %d AND %d", $minRow, $maxRow);
         $q = DB\Table::Select($query, array());
         
         return $q;
      }

      public function getById($id)
      {
         $items = $this->find("ACCOUNT_ID = :id", array(":id" => $id), 0, 1);

         if (!empty($items))
         {
            return $items[0];
         }

         return NULL;
      }

      public function createRecord()
      {
         return DB\Table::NewRecord();
      }

      public function updateRecord($id, $record)
      {
         return DB\Table::Update($id, $record);
      }

      public function getAccountRights($id)
      {
         $query =
            "SELECT
              R.*
             FROM
              SEC_ACCOUNTS_AUTHOR A,
              MCS_RIGHTS R,
              SEC_ACCOUNTS S
             WHERE
              A.ACCOUNT_ID = S.ACCOUNT_ID AND
              A.AUTH_CODE = R.CODE(+) AND
              S.ACCOUNT_ID = :id";

         $q = DB\Table::Select($query, array(":id" => $id));

         return $q;
      }
   }
   DB\Aurora::registerRepository("AccountsRepository", "\\DB\\Sec\\AccountsRepository");

   
   class RigtsRepository implements DB\RepositoryInterface
   {
      private $columns = "RIGHT_ID,
         LEVEL1,
         LEVEL2,
         LEVEL3,
         CODE,
         REFERENCE,
         DESCRIPTION";

      public function getById($id)
      {
         $items = $this->find("RIGHT_ID = :id", array(":id" => $id), 0, 1);

         if (!empty($items))
         {
            return $items[0];
         }

         return NULL;
      }

      public function find($where, $args, $from, $count)
      {
         $minRow = $from;
         $maxRow = $from + $count;

         $query = sprintf("SELECT z2.* FROM ( SELECT z1.*, ROWNUM AS zend_db_rownum FROM (

    SELECT {$this->columns} FROM MCS_RIGHTS
    WHERE ($where)

   ) z1 ) z2 WHERE z2.zend_db_rownum BETWEEN %d AND %d", $minRow, $maxRow);
         $q = DB\Table::Select($query, $args);

         return $q;
      }

      public function findAll($from, $count)
      {
         $minRow = $from;
         $maxRow = $from + $count;

         $query = sprintf("SELECT z2.* FROM ( SELECT z1.*, ROWNUM AS zend_db_rownum FROM (

    SELECT {$this->columns} FROM MCS_RIGHTS

   ) z1 ) z2 WHERE z2.zend_db_rownum BETWEEN %d AND %d", $minRow, $maxRow);
         $q = DB\Table::Select($query, array());

         return $q;
      }

      public function createRecord()
      {
         $record = DB\Table::NewRecord();

         return $record;
      }

      public function updateRecord($id, $record)
      {
         return DB\Table::Update($id, $record);
      }
   }
   DB\Aurora::registerRepository("RigtsRepository", "\\DB\\Sec\\RigtsRepository");

   class RolesRepository implements DB\RepositoryInterface
   {
      private $columns = "id,
         Subject,
         Group,
         Role,
         Name,
         Create,
         Read,
         Update,
         Delete";

      public function getById($id)
      {
         $items = $this->find("id = :id", array(":id" => $id), 0, 1);

         if (!empty($items))
         {
            return $items[0];
         }

         return NULL;
      }

      public function find($where, $args, $from=0, $count=0)
      {
         $minRow = $from;
         $maxRow = $from + $count;

         $query = sprintf("SELECT {$this->columns} FROM Rights
            WHERE ($where)");
         $q = DB\Table::Select($query, $args);

         return $q;
      }

      public function findAll($from=0, $count=0)
      {
         $query = sprintf("SELECT {$this->columns} FROM Rights");
         $q = DB\Table::Select($query, array());

         return $q;
      }

      public function createRecord()
      {
         $record = DB\Table::NewRecord();

         return $record;
      }

      public function updateRecord($id, $record)
      {
         return DB\Table::Update($id, $record);
      }
   }
   DB\Aurora::registerRepository("RolesRepository", "\\DB\\Sec\\RolesRepository");

   class WebAccountsRepository implements DB\RepositoryInterface
   {
      private $columns = "WEB_ACCOUNT_ID,
         Group,
         Role,
         Name,
         Create,
         Read,
         Update,
         Delete";

      public function getById($id)
      {
         $items = $this->find("id = :id", array(":id" => $id), 0, 1);

         if (!empty($items))
         {
            return $items[0];
         }

         return NULL;
      }

      public function find($where, $args, $from=0, $count=0)
      {
         $minRow = $from;
         $maxRow = $from + $count;

         $query = sprintf("SELECT {$this->columns} FROM WEB_ACCOUNTS
            WHERE ($where)");
         $q = DB\Table::Select($query, $args);

         return $q;
      }

      public function findAll($from=0, $count=0)
      {
         $query = sprintf("SELECT {$this->columns} FROM WEB_ACCOUNTS");
         $q = DB\Table::Select($query, array());

         return $q;
      }

      public function createRecord()
      {
         $record = DB\Table::NewRecord();

         return $record;
      }

      public function updateRecord($id, $record)
      {
         return DB\Table::Update($id, $record);
      }
   }
   DB\Aurora::registerRepository("RolesRepository", "\\DB\\Sec\\RolesRepository");
}
